package com.fitsoftwareprocess.mobilemap;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.fitsoftwareprocess.mobilemap.ListAddressActivity.HomeInfoExItemAdapter;
import com.fitsoftwareprocess.mobilemap.bus.MyWebClient;
import com.fitsoftwareprocess.mobilemap.bus.ServiceFinder;
import com.fitsoftwareprocess.mobilemap.bus.ShortestPathFinder;
import com.fitsoftwareprocess.mobimap.dto.Coordinate;
import com.fitsoftwareprocess.mobimap.dto.HomeInfoEx;
import com.fitsoftwareprocess.mobimap.dto.PlaceInfo;
import com.fitsoftwareprocess.mobimap.dto.PlaceTypeInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListServiceActivity extends Activity {
	ListView mlistView;
	String keyword;
	List<PlaceInfo> listPlaceInfo = new ArrayList<PlaceInfo>();
	private String fromActivity;
	private String endKeyWord;
	private String startLong;
	private String startLat;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        keyword = getIntent().getExtras().getString("keyword");
        fromActivity = getIntent().getExtras().getString("from");
		endKeyWord = getIntent().getExtras().getString("endkeyword");
		startLong = getIntent().getExtras().getString("long");
		startLat = getIntent().getExtras().getString("lat");
		
        setContentView(R.layout.activity_listservice);
        mlistView = (ListView) findViewById(R.id.listaddress_ListService);
		ServiceFinder serviceFinder = new ServiceFinder();
		listPlaceInfo = serviceFinder.GetListService(keyword);
		mlistView.setAdapter(new PlaceInfoItemAdapter(this,
  				android.R.layout.simple_list_item_1,listPlaceInfo));
		mlistView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				PlaceInfo temp = (PlaceInfo) mlistView.getItemAtPosition(arg2);
				Intent intent;
				String longitude = temp.get_location().get_longitude()+"";
				String latitude = temp.get_location().get_latitude()+"";
				if(fromActivity.equals("ResultStartPointActivity")){
					intent = new Intent(ListServiceActivity.this,
							ResultEndPointActivity.class);
					intent.putExtra("long", longitude);
					intent.putExtra("lat", latitude);
					intent.putExtra("endkeyword", endKeyWord);
					startActivity(intent);
				}else if(fromActivity.equals("ResultEndPointActivity")){
					//Tim duong
					ShortestPathFinder shortestPathFinder = new ShortestPathFinder();
					ArrayList<Coordinate> direction  = shortestPathFinder.FindShortestPath(new Coordinate(Double.parseDouble(startLat), Double.parseDouble(startLong)),
							new Coordinate(Double.parseDouble(latitude), Double.parseDouble(longitude)));
					intent = new Intent(ListServiceActivity.this,
							MainActivity.class);
					intent.putExtra("long",longitude);
					intent.putExtra("lat",latitude);
					intent.putExtra("direction", direction);
					startActivity(intent);
				}else{
					intent = new Intent(ListServiceActivity.this,MainActivity.class);
					intent.putExtra("long",longitude);
					intent.putExtra("lat",latitude);
					startActivity(intent);
				}
			}
		});
	}
	
	class PlaceInfoItemAdapter extends ArrayAdapter<PlaceInfo> {
		private List<PlaceInfo> list;

		PlaceInfoItemAdapter(Context context, int textViewResourceId,
				List<PlaceInfo> list) {
			super(context, textViewResourceId, list);
			this.list = list;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.listserviceitem, null);
			}

			PlaceInfo placeInfo = list.get(position);
			if (list != null) {
				TextView id = (TextView) view.findViewById(R.id.iteservicefirts);
				TextView name = (TextView) view.findViewById(R.id.itemserivesecond);
	
				
					
					id.setText(placeInfo.get_address());
					name.setText(placeInfo.get_fullName());		

			}

			return view;

		}
	}


}
